<?php

namespace App\Console\Commands\OpenShop;

use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Models\OpenShop\InvestmentPromotion\OpenShopOrganizationManageModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderPermissionAnchorModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSiteMissionModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserBaseInfoModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserExtContractModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationModel;
use App\Models\OpenShop\StartBusiness\OpenShopStartBusinessModel;
use App\Service\Guanyuan\CurlService;
use App\Service\OpenShop\Feishu\OrganizationManageService;
use App\Service\OpenShop\Feishu\ShopManualService;
use App\Service\OpenShop\OrderService;
use App\Service\OpenShop\Statistical\SignOrderStatisticalService;
use App\Support\OrganizationManageFsRobotSendSupport;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class OpenShopOrderSignStatisticalMonth extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'open_shop:order_sign_statistical_month';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '开店订单每周四驱群通知 .';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // 获取所有数据 - 本周新签门店
        $startTimestamp = strtotime(date('Y-m'));
        // 本周日
        $endTimestamp = strtotime(date('Y-m', strtotime('+1 month'))) - 1;

        // 获取数据模型
        $signOrderService = new SignOrderStatisticalService();
        $signOrderService->setTimeInterval($startTimestamp, $endTimestamp);

        $dataSource = [
            'newWaitLocationList'            => $signOrderService->getNewWaitLocationCount(),
            'waitLocationList'               => $signOrderService->getWaitLocationCount(),
            'waitLocationListTotal'          => $signOrderService->getWaitLocationCount(true),
            'waitLocationListFirstShopTotal' => $signOrderService->getWaitLocationCount(true, true),
            'waitMerchantsLocationList'      => $signOrderService->getWaitMerchantsLocationCount(),
            'waitPreparationsList'           => $signOrderService->getWaitPreparationsCount(),
            'waitPreparationsListTotal'      => $signOrderService->getWaitPreparationsCount(true),
            'gyStatusWeekList'               => $signOrderService->getGYStatusWeek(true),
            'gyAnnualShopData'               => $signOrderService->getGyAnnualShopData(true)
        ];


        $allArea = $signOrderService->getInitAreaList([
            'newSignNum', // 新签门店数
            'newWaitLocationCount',  // 新进选址门店
            'waitLocationNumTotal',
            'waitLocationNumTotalFirstShopTotal', // 本周首店选址中门店数

            // 观远字段
            'newOpenShopNumMonthGyWeek', // 本月新开-周数据

            'waitMerchantsLocationNum', // 落址门店数
            'waitPreparationsNumTotal',
            'waitPreparationsNum', // 筹备中的店数
            'newSignGWNumMonth', // 本月新签约
            'newOpenShopNumMonth', // 本月新开门店数
            'stillOpenShopNumMonth', // 本月在营门店数
            'newOpenTargetNumMonth',
            'sumOpenShopNum',
            'gwStillOpenShopTargetNumYear',
            'suspendedOpenShopNumMonth',
            'closedShopNumYear',
            'closedShopNumLimitYear',
            'preparationsNumWeek' // 本周筹备
        ]);

        foreach ($dataSource as $dsValue) {
            foreach ($dsValue as $dsvValue) {
                $code                            = $dsvValue['provinceCode'];
                $allArea[$code]['templateValue'] = array_merge($allArea[$code]['templateValue'],
                    $dsvValue['templateValue']);
            }
        }

        $resultFormatData = $signOrderService->formatCompanyDataSend($allArea);

        $ytd = SignOrderStatisticalService::getYTD();
        $mtd = SignOrderStatisticalService::getMTD();
        // 推送数据
        foreach ($resultFormatData as $companyDatum) {
            $templateValue = $companyDatum['templateValue'];
            $percent1      = bcdiv($templateValue['newOpenShopNumMonthGyWeek'],
                    $templateValue['newOpenTargetNumMonth'] ?: '0.01', 4) * 100;
            $percent2      = bcdiv($templateValue['sumOpenShopNum'],
                    $templateValue['gwStillOpenShopTargetNumYear'] ?: '0.01', 4) * 100;

            // 多店未落址门店数 = 累计选址中门店数 - 正在选址中门店数
            $num1 = $templateValue['waitLocationNumTotal'] - $templateValue['waitLocationNumTotalFirstShopTotal'];

            $contentTemp = <<<STR
数据指标
本月新签的门店数: {$templateValue['newSignGWNumMonth']} 家
本月新进选址门店: {$templateValue['newWaitLocationCount']} 家
本月落址的门店数: {$templateValue['waitMerchantsLocationNum']} 家
本月新进筹备门店: {$templateValue['waitMerchantsLocationNum']} 家
本月新开的门店数: {$templateValue['newOpenShopNumMonthGyWeek']} 家
有效在营的门店数: {$templateValue['stillOpenShopNumMonth']} 家
暂停营业的门店数: {$templateValue['suspendedOpenShopNumMonth']} 家
---------------------------------
累计在营的门店数: {$templateValue['sumOpenShopNum']} 家
正在选址中门店数: {$templateValue['waitLocationNumTotalFirstShopTotal']} 家
多店未落址门店数: {$num1} 家
累计筹备中门店数: {$templateValue['preparationsNumWeek']} 家
---------------------------------
目标达成
本月新开目标: {$templateValue['newOpenTargetNumMonth']} 家
本月新开完成率: {$percent1}% 
MTD：{$mtd}%
本年在营目标: {$templateValue['gwStillOpenShopTargetNumYear']} 家
本年在营完成率: {$percent2}%
YTD：{$ytd}%
本年闭店门店数: {$templateValue['closedShopNumYear']} 家
本年闭店门店数上限: {$templateValue['closedShopNumLimitYear']} 家
STR;
//            ---------------------------------
//辅助数据
//累计选址中门店数: {$templateValue['waitLocationNumTotal']}
//本月新开门店数: {$templateValue['newOpenShopNumMonthGyWeek']}
//            if ($companyDatum['manageId'] != 10) {
//                continue;
//            }

//            $companyDatum['manageId'] = 99;
            OrganizationManageFsRobotSendSupport::send($companyDatum['manageId'],
                '[' . $companyDatum['name'] . '] 月数据 [目标追踪]', $contentTemp, 'feishuRobotDataDay');
        }
    }


}
